package com.lgx.mall.core.controller;


import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.json.JsonUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.core.entity.Brand;
import com.lgx.mall.core.service.IBrandService;


@Controller
@RequestMapping("/brand")
public class BrandController {
	
	@Autowired
	private IBrandService brandService;

	public void setBrandService(IBrandService brandService) {
		this.brandService = brandService;
	}
	
	@RequestMapping("/brandMgr.do")
	public String brandMgr(HttpServletRequest request,HttpServletResponse response)
	{
		return "mall/core/brand/brandMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response)
	{
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		Pagination page = this.brandService.findBrandPagination(new Object[]{}, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		
		Json2Utils.parseJSON(page, response);
	}
	
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response)
	{
		String type = request.getParameter("type");
		String brandId = request.getParameter("brandId");
		if(type.equals("edit")){
			Brand brand = this.brandService.findBrandById(Long.parseLong(brandId));
			request.setAttribute("brand", brand);
		}
		
		request.setAttribute("type", type);
		request.setAttribute("brandId", brandId);
		
		return "mall/core/brand/brand-init";
	}

	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response)
	{
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String desc = request.getParameter("description");
		if(!StringUtils.isEmpty(desc))desc = EscapeUtils.unescape(desc);
		String imageUrl = request.getParameter("imageUrl");
		if(!StringUtils.isEmpty(desc))imageUrl = EscapeUtils.unescape(imageUrl);
		
		Brand brand = new Brand();
		
		brand.setName(name);
		brand.setDescription(desc);
		brand.setImageUrl(imageUrl);
		
		try {
			this.brandService.createBrand(brand);
			return "1";
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response)
	{
		String brandId = request.getParameter("brandId");
		Brand brand = this.brandService.findBrandById(Long.parseLong(brandId));
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String desc = request.getParameter("description");
		if(!StringUtils.isEmpty(desc))desc = EscapeUtils.unescape(desc);
		String imageUrl = request.getParameter("imageUrl");
		if(!StringUtils.isEmpty(desc))imageUrl = EscapeUtils.unescape(imageUrl);
		
		brand.setName(name);
		brand.setDescription(desc);
		brand.setImageUrl(imageUrl);
		
		try {
			this.brandService.updateBrand(brand);
			return "1";
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response)
	{
		String ids = request.getParameter("ids");
		try {
			this.brandService.deleteBrands(ids.split(","));
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
	
	@RequestMapping("/listBrand.do")
	public void listBrand(HttpServletRequest request,HttpServletResponse response){
		
		List<Brand> br = this.brandService.findBrandList();
		
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(br);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
